/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.pay.db.impl;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.yunhoo.pay.model.AffairFee;
import com.yunhoo.pay.model.BaseFeeItem;
import com.yunhoo.pay.model.Expense;
import com.yunhoo.pay.model.StyleDetail;
import com.yunhoo.pay.model.TravelCost;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.LoginParam;
import com.yunhoo.sys.util.common.Null;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class TraveCostImpl {

    private static String nameSpace = "travelcost.";

    private static Map paramMap() {
        Map map = new HashMap();
        map.put("orgCode", LoginParam.getOrg().getCode());
        return map;
    }

    public static void insertTravelCost(Expense exp) {
        Map map = new HashMap();
        map.put("voutype", exp.getVoutype());
        map.put("numDate", exp.getNumDate());
        map.put("number", exp.getNumber());
        map.put("numberStatus", exp.getNumberStatus());
        map.put("projCode", Null.convert(exp.getProject().getFinCode()));
        map.put("affair", exp.getAffair());
        map.put("total", exp.getTotal());
        map.put("repayment", exp.getRepayment());
        map.put("remain", exp.getRemain());
        map.put("person", exp.getUser().getCpsn_number());
        map.put("deptcode", exp.getDept().getCode());
        map.put("beginDate", exp.getBeginDate());
        map.put("endDate", exp.getEndDate());
        map.put("payDept", exp.getPayDept().getCode());
        map.put("feePerson", exp.getFeePer().getCpsn_number().trim());
        //map.put("payStyle", exp.getPayStyle().getCode().trim());
        map.put("payStyle", "");
        map.put("affixAmount", exp.getAffixAmount());
        map.put("memo", exp.getMemo());
        IbatisTemplate.insert(nameSpace + "insertTravelCost", map);
    }

    public static void updateTravelCostByPerson(Expense exp) {
        Map map = new HashMap();
        map.put("number", exp.getNumber());
        map.put("numDate", exp.getNumDate());
        map.put("projCode", Null.convert(exp.getProject().getFinCode()));
        map.put("begindate", exp.getBeginDate());
        map.put("enddate", exp.getEndDate());
        map.put("affair", exp.getAffair());
        map.put("total", exp.getTotal());
        map.put("repayment", exp.getRepayment());
        map.put("remain", exp.getRemain());
        map.put("numberStatus", exp.getNumberStatus());
        map.put("payDept", exp.getPayDept().getCode());
        map.put("feePerson", exp.getFeePer().getCpsn_number().trim());
        map.put("payStyle", "");
        map.put("affixAmount", exp.getAffixAmount());
        map.put("memo", exp.getMemo());
        map.put("voutype", exp.getVoutype());
        IbatisTemplate.update(nameSpace + "updateTravelCost", map);
    }

    public static void insertTravelVoucher(SqlMapClient sqlmap, Expense exp) throws SQLException {
        Map map = paramMap();
        map.put("voutype", exp.getVoutype());
        map.put("numDate", exp.getNumDate());
        map.put("number", exp.getNumber());
        map.put("numberStatus", exp.getNumberStatus());
        map.put("projCode", Null.convert(exp.getProject().getFinCode()));
        map.put("affair", exp.getAffair());
        map.put("total", exp.getTotal());
        map.put("repayment", exp.getRepayment());
        map.put("remain", exp.getRemain());
        map.put("person", exp.getUser().getCpsn_number());
        map.put("deptcode", exp.getDept().getCode());
        map.put("beginDate", exp.getBeginDate());
        map.put("endDate", exp.getEndDate());
        map.put("payDept", exp.getPayDept().getCode());
        map.put("feePerson", exp.getFeePer().getCpsn_number().trim());
        //map.put("payStyle", exp.getPayStyle().getCode().trim());
        map.put("payStyle", "");
        map.put("affixAmount", exp.getAffixAmount());
        map.put("memo", exp.getMemo());
        map.put("chuChaiRen", exp.getChuChaiRen());
        sqlmap.insert(nameSpace + "insertTravelCost", map);
    }

    public static void updateTravelVoucher(SqlMapClient sqlmap, Expense exp) throws SQLException {
        Map map = paramMap();
        map.put("number", exp.getNumber());
        map.put("numDate", exp.getNumDate());
        map.put("projCode", Null.convert(exp.getProject().getFinCode()));
        map.put("begindate", exp.getBeginDate());
        map.put("enddate", exp.getEndDate());
        map.put("affair", exp.getAffair());
        map.put("total", exp.getTotal());
        map.put("repayment", exp.getRepayment());
        map.put("remain", exp.getRemain());
        map.put("numberStatus", exp.getNumberStatus());
        map.put("payDept", exp.getPayDept().getCode());
        map.put("feePerson", exp.getFeePer().getCpsn_number().trim());
        map.put("payStyle", "");
        map.put("affixAmount", exp.getAffixAmount());
        map.put("memo", exp.getMemo());
        map.put("voutype", exp.getVoutype());
        map.put("chuChaiRen", exp.getChuChaiRen());
        sqlmap.update(nameSpace + "updateTravelCost", map);
    }

    public static void insertTravelDetail(SqlMapClient client, Expense expense, List<TravelCost> li, AffairFee fee) throws SQLException {

        //System.out.println("li:" + li.size());
        for (TravelCost cost : li) {
            client.insert(nameSpace + "insertDetailItem", dealNullValueOfBaseFeeItem(cost));//插入交通费项目
            TravelCost travelnew = new TravelCost();
            //住宿
            travelnew.getTravelDetail().getHotelFee().setNumber(expense.getNumber());
            travelnew.getTravelDetail().getHotelFee().setRownumber(cost.getRownumber());
            travelnew.getTravelDetail().getHotelFee().setQuantity(cost.getTravelDetail().getHotelFee().getQuantity());
            travelnew.getTravelDetail().getHotelFee().setAmount(Null.convert(cost.getTravelDetail().getHotelFee().getAmount()));
            //出差补助
            travelnew.getTravelDetail().getTravelGrantsFee().setNumber(expense.getNumber());
            travelnew.getTravelDetail().getTravelGrantsFee().setRownumber(cost.getRownumber());
            travelnew.getTravelDetail().getTravelGrantsFee().setQuantity(cost.getTravelDetail().getTravelGrantsFee().getQuantity());
            travelnew.getTravelDetail().getTravelGrantsFee().setAmount(Null.convert(cost.getTravelDetail().getTravelGrantsFee().getAmount()));
            client.insert(nameSpace + "insertOtherFeeItem", dealNullValueOfBaseFeeItem(travelnew.getTravelDetail().getHotelFee()));//插入交通费项目
            client.insert(nameSpace + "insertOtherFeeItem", dealNullValueOfBaseFeeItem(travelnew.getTravelDetail().getTravelGrantsFee()));//插入交通费项目
        }
        client.insert(nameSpace + "insertOtherFeeItem", dealNullValueOfBaseFeeItem(fee.getOfficeFee()));//插入杂费
        client.insert(nameSpace + "insertOtherFeeItem", dealNullValueOfBaseFeeItem(fee.getOrderTicketFee()));//插入杂费
        client.insert(nameSpace + "insertOtherFeeItem", dealNullValueOfBaseFeeItem(fee.getCityTracfficFee()));//插入杂费
        client.insert(nameSpace + "insertOtherFeeItem", dealNullValueOfBaseFeeItem(fee.getOtherFee()));//插入杂费
    }

    public static void insertTravel(SqlMapClient client, Expense expense, List<TravelCost> li, AffairFee fee) throws SQLException {

        insertTravelVoucher(client, expense);//插入报销单目录
        insertTravelDetail(client, expense, li, fee);//插入明细和杂费
        client.update(nameSpace + "updatepayVouchers", expense);//单据总金额

    }

    private static BaseFeeItem dealNullValueOfBaseFeeItem(BaseFeeItem item) {
        if (item.getQuantityDesc() == null) {
            item.setQuantityDesc(0d);
        }
        if (item.getAmount() == null) {
            item.setAmount(0d);
        }
        if (item.getQuantity() == null) {
            item.setQuantity(0d);
        }
        if (item.getPrice() == null) {
            item.setPrice(0d);
        }
        if (item.getAmount() == null) {
            item.setAmount(0d);
        }
        if (item.getStdAmount() == null) {
            item.setStdAmount(0d);
        }
        if (item.getOverRate() == null) {
            item.setOverRate(0d);
        }
        if (item.getRatio() == null) {
            item.setRatio(0d);
        }
        if (item.getVouAmount() == null) {
            item.setVouAmount(0d);
        }
        if (item.getPayAmount() == null) {
            item.setPayAmount(0d);
        }
        return item;
    }

    public static List<TravelCost> traficDetail(String number) {
        Map map = paramMap();
        map.put("number", number);
        return IbatisTemplate.queryForList(nameSpace + "travelFeeByNumber", map);
    }

    public static List<BaseFeeItem> otherTraticFee(Map map) {
        return IbatisTemplate.queryForList(nameSpace + "otherTravelFeeByNumber", map);
    }

    public static void updateTravel(SqlMapClient client, Expense voucher, List<TravelCost> li, AffairFee fee) throws SQLException {
        Map map = paramMap();
        map.put("number", voucher.getNumber().trim());
        /* 交通费明细删除 */
        client.delete(nameSpace + "deleTravelDetail", map);

        /* 差旅杂费删除 */
        client.delete(nameSpace + "delOtherTravelFee", map);

        insertTravelDetail(client, voucher, li, fee);//插入明细和杂费

        updateTravelVoucher(client, voucher);//修改报销单
    }

    public static void deleteTravel(Expense voucher) {
        SqlMapClient client = IbatisTemplate.getSqlMapClient();
        try {
            client.startTransaction();
            client.startBatch();
            Map map = paramMap();
            map.put("voutype", "1");
            map.put("number", voucher.getNumber().trim());
            /* 报销单删除*/
            client.delete(nameSpace + "deleExpense", voucher);
            /* 交通费明细删除 */
            client.delete(nameSpace + "deleTravelDetail", map);

            /* 差旅杂费删除 */
            client.delete(nameSpace + "delOtherTravelFee", map);

            /* 删除付款明细 */
            client.delete(nameSpace + "deleteStyleDetail", map);

            client.executeBatch();
            client.commitTransaction();
        } catch (SQLException ex) {
            Logger.getLogger(TraveCostImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                client.endTransaction();
            } catch (SQLException ex) {
                Logger.getLogger(TraveCostImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static List<StyleDetail> queryStyleDetail(String number) {
        Map map = paramMap();
        map.put("number", number);
        map.put("vouType", "1");
        return IbatisTemplate.queryForList(nameSpace + "getStyleDetailList", map);
    }

    public static List<StyleDetail> queryStyleDetailList(String number) {
        Map map = paramMap();
        map.put("number", number);
        return IbatisTemplate.queryForList(nameSpace + "queryStyleDetailList", map);
    }

    private static String curOrgCode() {
        return LoginParam.getOrg().getCode();
    }
}
